package com.frank.oj.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.frank.model.entity.common.Announcement;
import com.frank.oj.common.result.CommonResult;
import com.frank.oj.model.vo.AnnouncementVO;
import com.frank.oj.service.admin.system.AnnouncementService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 公告
 * @author frank
 * @Date 2024/3/28
 */
@RestController
@RequiresAuthentication
@RequestMapping("/api/admin")
public class AnnouncementController {
    @Resource
    private AnnouncementService announcementService;

    @GetMapping("/announcement")
    @RequiresPermissions("announcement_admin")
    public CommonResult<IPage<AnnouncementVO>> getAnnouncementList(@RequestParam(value = "limit",required = false) Integer limit,
                                                                   @RequestParam(value = "currentPage",required = false) Integer currentPage){
        return CommonResult.successResponse(announcementService.getAnnouncementList(limit,currentPage));
    }

    /**
     * 添加公告
     * @param announcement 公告
     * @return null
     */
    @PostMapping("/announcement")
    @RequiresRoles("root")
    @RequiresPermissions("announcement_admin")
    public CommonResult<Void> addAnnouncement(@RequestBody Announcement announcement){
        try {
            announcementService.addAnnouncement(announcement);
            return CommonResult.successResponse();
        } catch (Exception e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }

    @DeleteMapping("/announcement")
    @RequiresPermissions("announcement_admin")
    public CommonResult<Void> deleteAnnouncement(@RequestParam("aid") Long aid) {
        try {
            announcementService.deleteAnnouncement(aid);
            return CommonResult.successResponse();
        }catch (Exception e){
            return CommonResult.errorResponse(e.getMessage());
        }
    }

    @PutMapping("/announcement")
    @RequiresPermissions("announcement_admin")
    public CommonResult<Void> updateAnnouncement(@RequestBody Announcement announcement) {
        try {
            announcementService.updateAnnouncement(announcement);
            return CommonResult.successResponse();
        }catch (Exception e){
            return CommonResult.errorResponse(e.getMessage());
        }
    }

}
